#! /usr/bin/python
# -*- coding: utf-8 -*-

##
 # create_random_lpbs.py
 #
 # Copyright 2012 Fabian Wenzelmann, Christian Schilling
 #
 # This file is part of Recognition-Procedures-for-Boolean-Functions.
 #
 # Recognition-Procedures-for-Boolean-Functions is free software: you
 # can redistribute it and/or modify it under the terms of the
 # GNU General Public License as published by the Free Software Foundation,
 # either version 3 of the License, or (at your option) any later version.
 #
 # Recognition-Procedures-for-Boolean-Functions is distributed in the hope
 # that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 # Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with Recognition-Procedures-for-Boolean-Functions.
 #
 # If not, see <http://www.gnu.org/licenses/>.
 #/

# This script creates random LPB examples of a given size.

import os.path
import random
import argparse

class LPB:
	def __init__(self, degree, coeffs=[]):
		self.coeffs = coeffs
		self.degree = degree
		
	def __repr__(self):
		result = ""
		result += str(len(self.coeffs))
		result += '\n'
		result += str(self.degree)
		result += '\n'
		for coeff in self.coeffs:
			result += str(coeff)
			result += '\n'
		return result

def main():
	parser = argparse.ArgumentParser(description='Creates random LPBs.')
	parser.add_argument("--num", nargs=1, type=int, required=True, help="The number of generated LPBs.")
	parser.add_argument("--out", nargs=1, help="Output directory.", default=".", required=False)
	parser.add_argument("--variables", nargs=1, type=int, help="Number of variables in the generated LPBs.", required=True)
	parser.add_argument("--max-degree", nargs=1, type=int, help="Maximum degree of the generated LPBs.", required=True)
	parser.add_argument("--max-coeff", nargs=1, type=int, help="Maximum coefficient in the generated LPBs.", required=True)
	args = parser.parse_args()
	randoms = create_randoms(args.num[0], args.variables[0], args.max_degree[0], args.max_coeff[0])
	_dir = args.out[0]
	for i, lpb in enumerate(randoms):
		path = os.path.join(_dir, "lpb" + str(args.variables[0]) + "_" + str(i) + ".lpb")
		with open(path, 'w') as f:
			f.write(str(lpb))

def create_randoms(num, variables, max_degree, max_coeff):
	result = []
	for i in range(num):
		result.append(create_random(variables, max_degree, max_coeff))
	return result
	
def create_random(variables, max_degree, max_coeff):
	coeffs = []
	degree = random.randint(1, max_degree)
	for i in range(variables):
		coeff = random.randint(1, min(degree, max_coeff))
		coeffs.append(coeff)
	return LPB(degree, coeffs)

if __name__ == '__main__':
	main()
